#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time   : 2022-06-06 17:51:04
# @Author : 搬砖客


import time
import os
import pytest

from utils.reports_utils.send_report_util import SendReport
from utils.process_files_utils.yaml_util import YamlUtil
from utils.process_files_utils.case_automatic_control import TestCaseAutomaticGeneration
from utils.process_files_utils.clean_files import del_report_file

report_switch = YamlUtil.read_config_yaml("report", "enable")
report_type = YamlUtil.read_config_yaml("report", "type")
dingTalk_switch = YamlUtil.read_config_yaml("switch", "dingTalk")
case_automatic_switch = YamlUtil.read_config_yaml("switch", "case_automatic")

if __name__ == '__main__':
    # 自动生成代码.配置文件控制是否自动调用
    if case_automatic_switch:
        TestCaseAutomaticGeneration().get_case_automatic()
    # 先删除报告文件，防止命令未生效文件残留，导致报告错误
    del_report_file()
    # 本地和jenkins生成
    if report_switch:
        if report_type == "local":
            pytest.main(['--clean-alluredir', '--alluredir=./temps'])
            time.sleep(2)
            os.system("allure generate ./temps -o ./report --clean")
            time.sleep(1)
            if dingTalk_switch:
                SendReport.send_ding()
        else:
            pytest.main(['--clean-alluredir', '--alluredir=./allure-results'])
            time.sleep(2)
            os.system("allure generate ./allure-results -o ./allure-report --clean")
            if dingTalk_switch:
                SendReport.send_ding()

    else:
        pytest.main()
